<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        .box1 {
            width: 0;
            height: 0;
            border-bottom: 20px solid red;
            border-left: 20px solid transparent;
            border-right: 20px solid transparent;
            border-top: 20px solid transparent;
        }
    </style>
</head>

<body>
    <div class="box1"></div>
    <script>
       let str = 'cbacdcbc';

        let f = str => {
          let stack = [str[0]];
          let ch = str[0];
          for (let i = 1; i < str.length; i++) {
            if (str[i] < ch && str.indexOf(ch, i) !== -1) {
              stack.pop();
              ch = str[i];
              stack.push(ch);
            } else {
              ch = str[i];
              stack.push(ch);
            }
          }
          return stack.join('');
        }

        console.log(f(str))
    </script>
</body>

</html>